package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.BusinessTrackRecordMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageResult<Business> ListBusinesses(BusinessDto businessDto) {
        Page<Business> page = this.baseMapper.listBusinesses(new Page<Business>(businessDto.getPage(), businessDto.getPageSize()), businessDto);
        return new PageResult<Business>(page.getTotal(), page.getRecords());

    }

    @Override
    public Business getBusinessById(Integer id) {
        return this.baseMapper.getBusinessById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void trackBusiness(Business business) {
        //修改业务状态
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        this.updateById(business);
        //添加跟踪记录
        BusinessTrackRecord trackRecord = new BusinessTrackRecord();
        trackRecord.setBusinessId(business.getId());
        trackRecord.setUserId(1);
        trackRecord.setUserId(CurrentUserHoler.getCurrentUser()); //当前登录用户ID
        trackRecord.setTrackStatus(business.getStatus());
        trackRecord.setKeyItems(business.getKeyItems().toString());
        trackRecord.setRecord(business.getRecord());
        trackRecord.setNextTime(business.getNextTime());
        trackRecord.setCreateTime(LocalDateTime.now());
        trackRecord.setTrackStatus(business.getTrackStatus());
        businessTrackRecordMapper.insert(trackRecord);
    }

    @Override
    public void toCustomer(Integer id) {
        Business business =baseMapper.getBusinessById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        baseMapper.updateById(business);
        // 添加客户
        Customer customer = new Customer();
        BeanUtils.copyProperties(business,customer);
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setUpdateTime(null);
        customer.setCourseId(null);
        customer.setCreateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }

    @Override
    public PageResult<Business> ListBusinessPool(BusinessDto businessDto) {
        Page<Business> page = new Page<>(businessDto.getPage(),businessDto.getPageSize());
        Page<Business> pageResult = businessMapper.ListBusinessPool(page,businessDto);
        return new PageResult<Business>(pageResult.getTotal(),pageResult.getRecords());
    }
}
